package com.reins.bookstore.daoimpl;

import com.reins.bookstore.dao.BasketDao;
import com.reins.bookstore.entity.Basketline;
import com.reins.bookstore.repository.BasketlineRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class BasketDaoImpl implements BasketDao {

    @Autowired
    BasketlineRepository basketlineRepository;

    @Override
    public List<Basketline> findAllBasketlinesByUserId(Integer id) {
        return basketlineRepository.findAllBasketlinesByUserId(id);
    }
    @Override
    public Basketline checkBasketline(Integer userId, Integer bookId){
        return basketlineRepository.checkBasketline(userId,bookId);
    }
    @Override
    public void deleteById(Integer id){
        basketlineRepository.deleteById(id);
    }
    @Override
    public Basketline save(Basketline add){
        return basketlineRepository.save(add);
    }
    @Override
    public Basketline findById(Integer id){
        return basketlineRepository.findById(id).get();
    }
    @Override
    public List<Basketline> findAll(){
        return basketlineRepository.findAll();
    }
    @Override
    public Integer deleteAllByUserId(Integer userId){
        List<Basketline> get_test = findAllBasketlinesByUserId(userId);
        if (get_test == null)
            return 0;
        else
        {
            for (Basketline del:get_test){
                basketlineRepository.deleteById(del.getBasketlineId());
            }
            return 1;
        }
    }
}
